package com.cdu.eduPanda.project.contoller;

import com.alibaba.excel.util.StringUtils;
import com.cdu.eduPanda.project.common.convention.result.Result;
import com.cdu.eduPanda.project.common.convention.result.Results;
import com.cdu.eduPanda.project.dao.entity.UserDO;
import com.cdu.eduPanda.project.dto.req.UserLoginReqDTO;
import com.cdu.eduPanda.project.dto.req.UserRegisterReqDTO;
import com.cdu.eduPanda.project.dto.resp.UserLoginRespDTO;
import com.cdu.eduPanda.project.service.UserService;
import com.cdu.eduPanda.project.utils.MailMsgUtil;
import jakarta.mail.MessagingException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
@Slf4j
public class UserController {

    private final UserService userService;

    /**
     * 获取注册验证码
     * @param email 注册邮箱
     * @return void
     */
    @GetMapping("/register/get-Captcha")
    public Result<Void> getCaptcha(@RequestParam("email") String email) {
        log.info("邮箱码：{}",email);
        userService.getCaptcha(email);
        return Results.success();
    }

    @PostMapping("/register")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam){
        userService.register(requestParam);
        return Results.success();
    }


    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam) {
        UserLoginRespDTO userLoginRespDTO = userService.login(requestParam);
        return Results.success(userLoginRespDTO);
    }

    /**
     * 判断用户是否登录
     */
    @GetMapping("/has-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username) {
        return Results.success(userService.checkLogin(username));
    }

    /**
     * 用户登出
     */
    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestParam("username") String username) {
        userService.logout(username);
        return Results.success();
    }
}
